package com.privat.einkaufsscan;

import java.util.List;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class EinkaufsScanActivity extends Activity {
	public DAO entrySource;
	private Boolean fastFlag = false;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		entrySource = new DAO(this);

		showMenu();

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			showMenu();
			return false;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {
				String contents = intent.getStringExtra("SCAN_RESULT");
				Entry tmpent = entrySource.getEntry(contents);
				if (tmpent != null) {
					if (fastFlag) {
						entrySource
								.updateEntry(contents, tmpent.getCount() + 1);
						startScanner();
					} else {
						buildEingabe(true);
						EditText code = (EditText) findViewById(R.id.CodeText);
						EditText produktObj = (EditText) findViewById(R.id.ProduktText);
						EditText ortObj = (EditText) findViewById(R.id.OrtText);
						produktObj.setText(tmpent.getProdukt());
						ortObj.setText(tmpent.getOrt());
						code.setText(contents);
					}
				} else {
					buildEingabe(true);
					EditText code = (EditText) findViewById(R.id.CodeText);
					code.setText(contents);
				}
			} else if (resultCode == RESULT_CANCELED) {
				buildEingabe(true);

			}

		} else
			System.out.println(requestCode);
	}

	private void buildEingabe(Boolean scan) {
		setContentView(R.layout.eingabescreen);
		Button saveScan = (Button) findViewById(R.id.ScanSaveButton);
		saveScan.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				EditText codeObj = (EditText) findViewById(R.id.CodeText);
				String codeStr = codeObj.getText().toString();
				EditText produktObj = (EditText) findViewById(R.id.ProduktText);
				String produktStr = produktObj.getText().toString();
				EditText ortObj = (EditText) findViewById(R.id.OrtText);
				String ortStr = ortObj.getText().toString();
				EditText countObj = (EditText) findViewById(R.id.CountText);
				String countStr = countObj.getText().toString();
				Context context = getApplicationContext();
				if (!produktStr.equals("") && !ortStr.equals("")
						&& !countStr.equals("")) {
					Entry tmpent = entrySource.getEntry(codeStr);
					if (tmpent != null) {
						entrySource.updateEntry(codeStr, tmpent.getCount()
								+ Integer.parseInt(countStr));
						Toast.makeText(context, "Produkt wurde aktualisiert.",
								Toast.LENGTH_SHORT).show();
					} else {
						entrySource.createEntry(codeStr, produktStr, ortStr,
								tmpent.getCount() + Integer.parseInt(countStr));
						Toast.makeText(context, "Produkt wurde gespeichert.",
								Toast.LENGTH_SHORT).show();
					}
					codeObj.setText("");
					produktObj.setText("");
					ortObj.setText("");
					countObj.setText("1");
					startScanner();
				} else {
					Toast.makeText(context,
							"Bitte die unteren drei Felder ausfuellen.",
							Toast.LENGTH_SHORT).show();
				}
			}

		});

		Button saveMenu = (Button) findViewById(R.id.ScanMenuButton);
		saveMenu.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				EditText codeObj = (EditText) findViewById(R.id.CodeText);
				String codeStr = codeObj.getText().toString();
				EditText produktObj = (EditText) findViewById(R.id.ProduktText);
				String produktStr = produktObj.getText().toString();
				EditText ortObj = (EditText) findViewById(R.id.OrtText);
				String ortStr = ortObj.getText().toString();
				EditText countObj = (EditText) findViewById(R.id.CountText);
				String countStr = countObj.getText().toString();
				Context context = getApplicationContext();
				if (!produktStr.equals("") && !ortStr.equals("")
						&& !countStr.equals("")) {
					Entry tmpent = entrySource.getEntry(codeStr);
					if (tmpent != null) {
						entrySource.updateEntry(codeStr, tmpent.getCount()
								+ Integer.parseInt(countStr));
						Toast.makeText(context, "Produkt wurde aktualisiert.",
								Toast.LENGTH_SHORT).show();
					} else {
						entrySource.createEntry(codeStr, produktStr, ortStr,
								Integer.parseInt(countStr));
						Toast.makeText(context, "Produkt wurde gespeichert.",
								Toast.LENGTH_SHORT).show();
					}
					codeObj.setText("");
					produktObj.setText("");
					ortObj.setText("");
					countObj.setText("1");
					showMenu();
				} else {
					Toast.makeText(context,
							"Bitte die unteren drei Felder ausfuellen.",
							Toast.LENGTH_SHORT).show();
				}
			}
		});

		Button cancel = (Button) findViewById(R.id.CancelButton);
		cancel.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Context context = getApplicationContext();
				Toast.makeText(context, "Scanvorgang wurde abgebrochen.",
						Toast.LENGTH_SHORT).show();
				showMenu();
			}
		});
	}

	private void showList() {
		setContentView(R.layout.produktlist);
		final ListView list = (ListView) findViewById(R.id.ListViewLayout);
		List<Entry> values = entrySource.getAllExisting();
		ArrayAdapter<Entry> adapter = new ArrayAdapter<Entry>(this,
				android.R.layout.simple_list_item_1, values);
		list.setAdapter(adapter);
		list.setClickable(true);
		list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				List<Entry> values = entrySource.getAllExisting();
				Entry tmpent = values.get(position);
				entrySource.updateEntry(tmpent.getCode(), tmpent.getCount() - 1);
				showList();
			}
		});
	}

	private void showMenu() {
		setContentView(R.layout.main);

		try {

			Button scanbut = (Button) findViewById(R.id.ScanButton);
			scanbut.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					fastFlag = false;
					startScanner();

				}

			});

			Button fastbut = (Button) findViewById(R.id.SchnellScanButton);
			fastbut.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					fastFlag = true;
					startScanner();

				}

			});

			Button exitbut = (Button) findViewById(R.id.ExitButton);
			exitbut.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					android.os.Process.killProcess(android.os.Process.myPid());

				}

			});

		} catch (ActivityNotFoundException anfe) {
			Context context = getApplicationContext();
			Toast.makeText(
					context,
					"Es muss ein BarcodeScanner installiert sein.\n Vorgang abgebrochen!",
					Toast.LENGTH_SHORT).show();
		}

		Button list = (Button) findViewById(R.id.ListButton);
		list.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				showList();
			}

		});
	}

	private void startScanner() {
		Intent intent = new Intent("com.google.zxing.client.android.SCAN");
		intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
		startActivityForResult(intent, 0);
	}

	@Override
	protected void onResume() {
		// entrySource.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		// entrySource.close();
		super.onPause();
	}
}
